// 路径: Server/src/main/java/com/hunterai/security/CustomPasswordEncoder.java

package com.hunterai.security;

import com.hunterai.util.PasswordUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class CustomPasswordEncoder implements PasswordEncoder {

    private final BCryptPasswordEncoder bCryptEncoder = new BCryptPasswordEncoder();
    
    @Override
    public String encode(CharSequence rawPassword) {
        return bCryptEncoder.encode(rawPassword);
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        // 首先尝试BCrypt匹配
        if (bCryptEncoder.matches(rawPassword, encodedPassword)) {
            return true;
        }
        
        // 如果是旧的Shiro密码格式
        if (encodedPassword.contains(":")) {
            return PasswordUtil.verifyPassword(rawPassword.toString(), encodedPassword);
        }
        
        return false;
    }
}